package com.hfxt.dao;

import com.hfxt.entity.Role;
import com.hfxt.entity.Users;
import com.hfxt.tool.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class RoleDaoImpl implements  RoleDao {


    public Role getRole(Integer roleid) {
        SqlSession sqlSession=null;
        Role role=null;
        try {
            sqlSession= MyBatisUtil.getSession();
            role=sqlSession.getMapper(RoleDao.class).getRole(roleid);
            return role;
        }catch (Exception e){
            e.printStackTrace();
            return  null;
        }finally {
            MyBatisUtil.closeSession();
        }


    }

    @Override
    public int addRole(Role role) {
        SqlSession sqlSession=null;
        int result=0;
        try {
            sqlSession= MyBatisUtil.getSession();
            result=sqlSession.getMapper(RoleDao.class).addRole(role);
            sqlSession.commit();
            return result;
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
            return  -1;
        }finally {
            MyBatisUtil.closeSession();
        }
    }

    @Override
    public int delRole(Integer roleid) {
        SqlSession sqlSession=null;
        int result=0;
        try {
            sqlSession= MyBatisUtil.getSession();
            result=sqlSession.getMapper(RoleDao.class).delRole(roleid);
            sqlSession.commit();
            return  result;
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
            return  -1;
        }finally {
            MyBatisUtil.closeSession();
        }
    }

    @Override
    public int updateRole(Role role) {
        SqlSession sqlSession=null;
        int result=0;
        try {
            sqlSession= MyBatisUtil.getSession();
            result=sqlSession.getMapper(RoleDao.class).updateRole(role);
            sqlSession.commit();
            return  result;
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
            return  -1;
        }finally {
            MyBatisUtil.closeSession();
        }
    }
}
